<?php

namespace App\Http\Controllers;

use App\Models\About;
use App\Models\Admin;
use App\Models\AdminOperate;
use App\Models\Article;
use App\Models\ArticleType;
use App\Models\Home;
use App\Models\Picture;
use App\Models\SiteInfo;
use App\Models\Story;
use App\Models\Test;
use App\Models\TestResult;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Session;

class AdminController extends BaseController
{

    public function login(){
        return view('admin/login');
    }

    public function doLogin(Request $request){
        $admin=Admin::where(['name'=>$_POST['name'],'pwd'=>$_POST['pass']])->first();
        if($admin!=null){
            $request->session()->put('admin', $admin);
            Log::info('Showing user profile for user: '.Session::get('admin'));

            //echo Session::get('loginType');
            echo "登陆成功";
        }else{
            echo "请输入正确的用户名或密码";
        }
        /*$admin=Admin::where(['name'=>$_POST['name'],'pwd'=>$_POST['pass']])->first();
        if($admin!=null){
            Session::put('admin',$admin);
            $request->session()->put('admin', $admin);
            Log::info('Showing user profile for user111: '.$request->session()->get('admin'));
            //echo Session::get('loginType');
            //return view('admin/index');
            return redirect()->action('AdminController@index');
        }else{
            return view('admin/login');
        }*/

    }

    public function index(){

        return view('admin/articleManage');
    }


    public function admin(){
        /*if(!$request->session()->has('admin')){
            return view('admin/login');
        }*/
        return view('admin/adminManage');
    }

    public function getAllAdmin(){
        $admins=Admin::orderBy('created_at', 'desc')->get();
        $admin_json=json_encode($admins);
        echo $admin_json;
    }

    public function addAdmin(){
        return view('admin/addAdmin');
    }

    public function saveAdmin(Request $request){
        $admin=new Admin;
        $admin->name=$request->input('name');
        $admin->pwd=$request->input('pwd');
        $admin->type=0;

        $admin->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='新增了管理员'.$admin->name;
        $adminOperate->save();

        return redirect()->action('AdminController@admin');
    }

    public function deleteAdmin(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $admin = Admin::find($row['id']);
            $admin->delete();

            $adminOperate=new AdminOperate;
            $adminOperate->adminId=Session::get('admin')->id;
            $adminOperate->adminName=Session::get('admin')->name;
            $adminOperate->content='删除了管理员'.$admin->name;
            $adminOperate->save();
        }
        echo 'success';
    }
    public function updateAdmin(){
        $id=$_GET['id'];
        $admin=Admin::find($id);
        return View('admin/updateAdmin')->with('admin',$admin);
    }
    public function doUpdateAdmin(Request $request){
        $admin=Admin::find($request->input('id'));
        $admin->name=$request->input('name');
        $admin->pwd=$request->input('pwd');
        $admin->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='更新了管理员'.$admin->name;
        $adminOperate->save();
        return redirect()->action('AdminController@admin');
    }

    public function adminOperate(Request $request){
        /*if(!$request->session()->has('admin')){
            return view('admin/login');
        }*/
        return view('admin/adminOperateManage');
    }

    public function getAllAdminOperate(){
        $admins=AdminOperate::orderBy('created_at', 'desc')->get();
        $admin_json=json_encode($admins);
        echo $admin_json;
    }


    public function article(){
        return view('admin/articleManage');
    }

    public function getAllArticle(){
        $articles=Article::orderBy('created_at', 'desc')->get();
        $article_json=json_encode($articles);
        echo $article_json;
    }
    public function addArticle(){
        $articleTypes=ArticleType::all();
        return view('admin/addArticle')->with('articleTypes',$articleTypes);
    }
    public function saveArticle(Request $request){
        $article=new Article;
        $article->title=$request->input('title');
        $article->keywords=$request->input('keywords');
        /*if($request->input('description')==null){
            $txt=$this->HtmlToText($request->input('content'));
            $short_txt=substr($txt,0,25).'...';
            $article->description=$short_txt;
        }else{
            $article->description=$request->input('description');
        }*/
        $article->description=$request->input('description');
        $article->typeId=$request->input('type');
        if($request->input('imgurl')!=null){
            $article->imgurl=$request->input('imgurl');
            $article->imgType=1;
        }else{
            $article->imgurl=$request->input('imgurl');
            $article->imgType=0;
        }
        $article->content=$request->input('content');
        $article->save();

        $home=new Home;
        $home->itemId=$article->id;
        $home->title=$article->title;
        $home->description=$article->description;
        $home->typeId=0;
        if($article->imgurl!=null){
            $home->imgurl=$article->imgurl;
            $home->imgType=1;
        }else{
            $home->imgurl=null;
            $home->imgType=0;
        }
        $home->url='/storyDetail/'.$article->id.'.html';
        $home->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='新增了文章'.$article->title;
        $adminOperate->save();

        return redirect()->action('AdminController@article');
    }

    public function testEditor(){
        $article=Article::find(7);

        return view('index/articleDetail')->with('article',$article);
    }
    public function deleteArticle(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $article = Article::find($row['id']);
            $article->delete();
            $adminOperate=new AdminOperate;
            $adminOperate->adminId=Session::get('admin')->id;
            $adminOperate->adminName=Session::get('admin')->name;
            $adminOperate->content='删除了文章'.$article->title;
            $adminOperate->save();

            $home=Home::where(['itemId'=>$row['id'],'typeId'=>0])->first();
            if($home!=null){
                $home->delete();
            }
        }
        echo 'success';
    }
    public function updateArticle(){
        $id=$_GET['id'];
        $article=Article::find($id);
        $articleTypes=ArticleType::all();
        return View('admin/updateArticle')->with('article',$article)->with('articleTypes',$articleTypes);
    }
    public function doUpdateArticle(Request $request){
        $article=Article::find($request->input('id'));
        $article->title=$request->input('title');
        $article->keywords=$request->input('keywords');
        $article->typeId=$request->input('type');
        $article->description=$request->input('description');
        if($request->input('imgurl')!=null){
            $article->imgurl=$request->input('imgurl');
            $article->imgType=1;
        }else{
            $article->imgurl=$request->input('imgurl');
            $article->imgType=0;
        }
        $article->content=$request->input('content');
        $article->save();

        $home=Home::where(['itemId'=>$request->input('id'),'typeId'=>0])->first();
        if($home!=null){
            $home->title=$article->title;
            $home->description=$article->description;
            if($article->imgurl!=null){
                $home->imgurl=$article->imgurl;
                $home->imgType=1;
            }else{
                $home->imgurl=null;
                $home->imgType=0;
            }
            $home->save();
        }else{
            $home=new Home;
            $home->itemId=$article->id;
            $home->title=$article->title;
            $home->description=$article->description;
            $home->typeId=0;
            if($article->imgurl!=null){
                $home->imgurl=$article->imgurl;
                $home->imgType=1;
            }else{
                $home->imgurl=null;
                $home->imgType=0;
            }
            $home->url='/storyDetail/'.$article->id.'.html';
            $home->save();
        }



        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='更新了文章'.$article->title;
        $adminOperate->save();
        return redirect()->action('AdminController@article');
    }

    public function articleType(){
        return view('admin/articleTypeManage');
    }

    public function getAllArticleType(){
        $articleTypes=ArticleType::orderBy('created_at', 'desc')->get();
        $articletype_json=json_encode($articleTypes);
        echo $articletype_json;
    }
    public function addArticleType(){
        return view('admin/addArticleType');
    }
    public function saveArticleType(Request $request){
        $articleType=new ArticleType;
        $articleType->name=$request->input('name');
        $articleType->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='新增了文章类型'.$articleType->name;
        $adminOperate->save();

        return redirect()->action('AdminController@articleType');
    }

    public function deleteArticleType(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $articleType = ArticleType::find($row['id']);
            $articleType->delete();

            $adminOperate=new AdminOperate;
            $adminOperate->adminId=Session::get('admin')->id;
            $adminOperate->adminName=Session::get('admin')->name;
            $adminOperate->content='删除了文章类型'.$articleType->name;
            $adminOperate->save();
        }
        echo 'success';
    }
    public function updateArticleType(){
        $id=$_GET['id'];
        $articleType=ArticleType::find($id);
        return View('admin/updateArticleType')->with('articleType',$articleType);
    }
    public function doUpdateArticleType(Request $request){
        $articleType=ArticleType::find($request->input('id'));
        $articleType->name=$request->input('name');
        $articleType->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='更新了文章类型'.$articleType->name;
        $adminOperate->save();
        return redirect()->action('AdminController@articleType');
    }



    public function picture(){
        return view('admin/pictureManage');
    }

    public function getAllPicture(){
        $pictures=Picture::orderBy('created_at', 'desc')->get();
        $picture_json=json_encode($pictures);
        echo $picture_json;
    }
    public function addPicture(){
        return view('admin/addPicture');
    }
    public function savePicture(Request $request){
        $picture=new Picture;
        $picture->title=$request->input('title');
        $picture->keywords=$request->input('keywords');
        $picture->description=$request->input('description');
        $url=$request->input('img');
        $imgurl=$request->input('imgurl');
        if($url==null){
            $picture->imgurl=$imgurl;
            $picture->type=1;
        }else{
            $picture->imgurl=$url;
            $picture->type=0;
        }
        $picture->save();

        $home=new Home;
        $home->itemId=$picture->id;
        $home->title=$picture->title;
        $home->description=$picture->description;
        $home->typeId=1;
        $home->imgurl=$picture->imgurl;
        $home->url='/pictureDetail/'.$picture->id.'.html';
        $home->save();


        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='新增了趣图'.$picture->title;
        $adminOperate->save();


        return redirect()->action('AdminController@picture');
    }

    public function deletePicture(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $picture = Picture::find($row['id']);
            $picture->delete();
            $adminOperate=new AdminOperate;
            $adminOperate->adminId=Session::get('admin')->id;
            $adminOperate->adminName=Session::get('admin')->name;
            $adminOperate->content='删除了趣图'.$picture->title;
            $adminOperate->save();

            $home=Home::where(['itemId'=>$row['id'],'typeId'=>1])->first();
            if($home!=null){
                $home->delete();
            }

        }
        echo 'success';
    }
    public function updatePicture(){
        $id=$_GET['id'];
        $picture=Picture::find($id);
        return View('admin/updatePicture')->with('picture',$picture);
    }
    public function doUpdatePicture(Request $request){
        $picture=Picture::find($request->input('id'));
        $picture->title=$request->input('title');
        $picture->keywords=$request->input('keywords');
        $picture->description=$request->input('description');
        $picture->imgurl=$request->input('imgurl');
        $picture->save();

        $home=Home::where(['itemId'=>$request->input('id'),'typeId'=>1])->first();
        if($home!=null){
            $home->title=$picture->title;
            $home->description=$picture->description;
            $home->imgurl=$picture->imgurl;
            $home->save();
        }else{
            $home=new Home;
            $home->itemId=$picture->id;
            $home->title=$picture->title;
            $home->description=$picture->description;
            $home->typeId=1;
            $home->imgurl=$picture->imgurl;
            $home->url='/pictureDetail/'.$picture->id.'.html';
            $home->save();
        }
        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='更新了趣图'.$picture->title;
        $adminOperate->save();
        return redirect()->action('AdminController@picture');
    }

    /*
     * 测试管理
     */
    public function test(){
        return view('admin/testManage');
    }

    public function getAllTest(){
        $tests=Test::orderBy('created_at', 'desc')->get();
        foreach( $tests as $test ){
            $test->result='<a class="btn btn-success" role="button" href="/admin/testResultManage?id='.$test->id.'">编辑查看</a>';
        }
        $test_json=json_encode($tests);
        echo $test_json;
    }
    public function addTest(){
        return view('admin/addTest');
    }
    public function saveTest(Request $request){
        $test=new Test;
        $test->title=$request->input('title');
        $test->description=$request->input('description');
        $test->imgurl=$request->input('imgurl');

        $test->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='新增了测试'.$test->title;
        $adminOperate->save();

        return redirect()->action('AdminController@test');
    }

    public function deleteTest(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $test = Test::find($row['id']);
            $test->delete();
            $adminOperate=new AdminOperate;
            $adminOperate->adminId=Session::get('admin')->id;
            $adminOperate->adminName=Session::get('admin')->name;
            $adminOperate->content='删除了测试'.$test->title;
            $adminOperate->save();
        }
        echo 'success';
    }
    public function updateTest(){
        $id=$_GET['id'];
        $test=Test::find($id);
        return View('admin/updateTest')->with('test',$test);
    }
    public function doUpdateTest(Request $request){
        $test=Test::find($request->input('id'));
        $test->title=$request->input('title');
        $test->description=$request->input('description');
        $test->imgurl=$request->input('imgurl');
        $test->save();
        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='更新了测试'.$test->title;
        $adminOperate->save();
        return redirect()->action('AdminController@test');
    }

    /*
     * 测试结果管理
     */
    public function testResult(){
        $id=$_GET['id'];
        return view('admin/testResultManage')->with('id',$id);
    }

    public function getAllTestResult(){
        $id=$_GET['id'];
        $testResults=TestResult::where('testId',$id)->get();
        $test_json=json_encode($testResults);
        echo $test_json;
    }
    public function addTestResult(){
        $id=$_GET['id'];
        return view('admin/addTestResult')->with('id',$id);
    }
    public function saveTestResult(Request $request){
        $testResult=new TestResult();
        $testResult->testId=$request->input('testId');
        $testResult->result=$request->input('result');

        $testResult->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='新增了测试结果'.$testResult->result;
        $adminOperate->save();

        return view('admin/testResultManage')->with('id',$request->input('testId'));
    }

    public function deleteTestResult(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $testResult = TestResult::find($row['id']);
            $testResult->delete();
            $adminOperate=new AdminOperate;
            $adminOperate->adminId=Session::get('admin')->id;
            $adminOperate->adminName=Session::get('admin')->name;
            $adminOperate->content='删除了测试结果'.$testResult->result;
            $adminOperate->save();
        }
        echo 'success';
    }
    public function updateTestResult(){
        $id=$_GET['id'];
        $testResult=TestResult::find($id);
        return View('admin/updateTestResult')->with('testResult',$testResult);
    }
    public function doUpdateTestResult(Request $request){
        $testResult=TestResult::find($request->input('id'));
        $testResult->result=$request->input('result');
        $testResult->save();
        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='更新了测试结果'.$testResult->result;
        $adminOperate->save();
        return view('admin/testResultManage')->with('id',$testResult->testId);
    }

    public function story(){
        return view('admin/storyManage');
    }

    public function getAllStory(){
        $storys=Story::all();
        $story_json=json_encode($storys);
        echo $story_json;
    }
    public function addStory(){
        return view('admin/addStory');
    }
    public function saveStory(Request $request){
        $story=new Story;
        $story->title=$request->input('title');
        $story->keywords=$request->input('keywords');
        $story->description=$request->input('description');
        if($request->input('imgurl')!=null){
            $story->imgurl=$request->input('imgurl');
            $story->type=1;
        }
        $story->content=$request->input('content');
        $story->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='新增了故事'.$story->title;
        $adminOperate->save();

        return redirect()->action('AdminController@story');
    }
    public function deleteStory(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $story = Story::find($row['id']);
            $story->delete();
            $adminOperate=new AdminOperate;
            $adminOperate->adminId=Session::get('admin')->id;
            $adminOperate->adminName=Session::get('admin')->name;
            $adminOperate->content='删除了故事'.$story->title;
            $adminOperate->save();
        }
        echo 'success';
    }
    public function updateStory(){
        $id=$_GET['id'];
        $story=Story::find($id);
        return View('admin/updateStory')->with('story',$story);
    }
    public function doUpdateStory(Request $request){
        $story=Story::find($request->input('id'));
        $story->title=$request->input('title');
        $story->keywords=$request->input('keywords');
        $story->description=$request->input('description');
        if($request->input('imgurl')!=null){
            $story->imgurl=$request->input('imgurl');
            $story->type=1;
        }else{
            $story->imgurl=$request->input('imgurl');
            $story->type=0;
        }
        $story->content=$request->input('content');
        $story->save();
        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='更新了故事'.$story->title;
        $adminOperate->save();
        return redirect()->action('AdminController@story');
    }

    public function about(){
        $about=SiteInfo::find(1);
        return view('admin/aboutManage')->with('about',$about);
    }
    public function saveAbout(Request $request){
        $about=SiteInfo::find(1);
        $about->content=$request->input('content');
        $about->save();

        $adminOperate=new AdminOperate;
        $adminOperate->adminId=Session::get('admin')->id;
        $adminOperate->adminName=Session::get('admin')->name;
        $adminOperate->content='修改了关于内容';
        $adminOperate->save();

        return redirect()->action('AdminController@about');
    }

    //html内容转化为纯文本
    public function HtmlToText($str){
        $str=preg_replace("/<sty(.*)/style>|<scr(.*)/script>|<!--(.*)-->/isU","",$str);//去除CSS样式、JS脚本、HTML注释
        $alltext="";//用于保存TXT文本的变量
        $start=1;//用于检测<左、>右标签的控制开关
        for($i=0;$i<strlen($str);$i++){//遍历经过处理后的字符串中的每一个字符
            if(($start==0)&&($str[$i]==">")){//如果检测到>右标签，则使用$start=1;开启截取功能
                $start=1;
            }else if($start==1){//截取功能
                if($str[$i]=="<"){//如果字符是<左标签，则使用<font color='red'>|</font>替换
                    $start=0;
                    $alltext.="<font color='red'>|</font>";
                }else if(ord($str[$i])>31){//如果字符是ASCII大于31的有效字符，则将字符添加到$alltext变量中
                    $alltext.=$str[$i];
                }
            }
        }
        //下方是去除空格和一些特殊字符的操作
        $alltext = str_replace("　"," ",$alltext);
        $alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
        $alltext = preg_replace("/[ ]+/s"," ",$alltext);
        return $alltext;
    }
}
